﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using FrameWork.Lib;

namespace AnimeWatchMax
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
        }

        public EventManager mEventManager = null;
        public ThreadManager mThreadManager = null;
        public UIManager mUIManager = null;
        public DataObjects.Data mData = null;


        private void btnSave_Click(object sender, EventArgs e)
        {
            mData.Save(mEventManager);
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            mData = DataObjects.Data.Load(mEventManager);
            mUIManager.InitData(mData);
            mThreadManager.InitData(mData);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //var xml = System.IO.File.ReadAllText(@"E:\BASE\DEVELOPEMNT\MaxTools\Apps\AnimeWatchMax\AnimeWatchMax\XSD\Animesuki.xml");
            //AnimeWatchMax.XSD.AnimeSukiHandler.ProcessAnimeSukiXMLToData(mEventManager, xml, mData);
            mThreadManager.StartUpdate();
        }

        private void btnUpdateRange_Click(object sender, EventArgs e)
        {
            mThreadManager.StartUpdateRange(txtUpdateRangeFromID.Text.IntValueOrDefault(1), txtUpdateRangeToID.Text.IntValueOrDefault(1));
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            mEventManager = new EventManager();
            mUIManager = new UIManager(mDockPanel, mEventManager);
            mThreadManager = new ThreadManager(mUIManager.EventManager);
            mUIManager.EventManager.UpdateProgress += new FrameWork.WinUI.EventManagerBase.DelegateUpdateProgress(EventManager_UpdateProgress);

            mUIManager.InitResultPanel();
            mUIManager.InitTitleListPanel();
            mUIManager.InitGroupListPanel();

            mUIManager.GroupListPanel.DockTo(mUIManager.TitleListPanel.Pane, DockStyle.Fill, -1);
            mUIManager.TitleListPanel.Activate();
            mUIManager.EventManager.AddTitleItem += new EventManager.DelegateAddTitleItem(mUIManager.TitleListPanel.AddOrUpdateItem);

            mData = DataObjects.Data.Load(mUIManager.EventManager);

            mUIManager.InitData(mData);

            mThreadManager.InitData(mData);
        }

        public SortedList<Guid, FrameWork.WinUI.Controls.StatusStripProgress> mStatusStripProgressList = new SortedList<Guid, FrameWork.WinUI.Controls.StatusStripProgress>();
        public void EventManager_UpdateProgress(Guid pKey, int pTotal, int pCurrent, string pDescription)
        {
            if (InvokeRequired)
            {
                this.Invoke(new FrameWork.WinUI.EventManagerBase.DelegateUpdateProgress(EventManager_UpdateProgress), pKey, pTotal, pCurrent, pDescription);
            }
            else
            {
                FrameWork.WinUI.Controls.StatusStripProgress tStatusStripProgress = null;

                if (mStatusStripProgressList.ContainsKey(pKey))
                { tStatusStripProgress = mStatusStripProgressList[pKey]; }
                else
                {
                    tStatusStripProgress = new FrameWork.WinUI.Controls.StatusStripProgress()
                    {
                        Key = pKey,
                        ToolStripLabel = new ToolStripLabel(pDescription),
                        ToolStripProgressBar = new ToolStripProgressBar()
                    };

                    mStatusBar.Items.Add(tStatusStripProgress.ToolStripLabel);
                    mStatusBar.Items.Add(tStatusStripProgress.ToolStripProgressBar);

                    mStatusStripProgressList.Add(pKey, tStatusStripProgress);
                }

                if ((pCurrent == 0 && pTotal == 0) || pCurrent == pTotal)
                {
                    mStatusBar.Items.Remove(tStatusStripProgress.ToolStripLabel);
                    mStatusBar.Items.Remove(tStatusStripProgress.ToolStripProgressBar);
                    mStatusStripProgressList.Remove(pKey);
                }
                else
                {
                    tStatusStripProgress.ToolStripProgressBar.Maximum = pTotal;
                    tStatusStripProgress.ToolStripProgressBar.Value = pCurrent % pTotal;
                    tStatusStripProgress.ToolStripLabel.Text = pDescription;
                }
            }
        }

        private void btnShowHideTitleListPane_Click(object sender, EventArgs e)
        {
            btnShowHideTitleListPane.Text = mUIManager.ShowHideTitleListPane();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            mThreadManager.StopUpdate();
            mThreadManager.StopUpdateRange();
            mThreadManager = null;

            mData.Save(mEventManager);

            mUIManager.EventManager.UpdateProgress -= new FrameWork.WinUI.EventManagerBase.DelegateUpdateProgress(EventManager_UpdateProgress);
            mUIManager.EventManager.AddTitleItem -= new EventManager.DelegateAddTitleItem(mUIManager.TitleListPanel.AddOrUpdateItem);
            mUIManager = null;
            mEventManager = null;
        }

        private void txtUpdateRangeToID_DoubleClick(object sender, EventArgs e)
        {
            var tList = mData.TitleList.OrderByDescending(t => t.ID).ToList();

            txtUpdateRangeToID.Text = tList.FirstOrDefault().ID.ToString();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }
    }
}
